<?php

namespace Admin\Controller;

class OrderController extends AdminController {
    
    /*
     * 订单列表
     */
    public function index(){
        //如果订单课程为0,将订单装修变成5
        if(I('get.tab')==3){
            $orderform = M("order_form");
            $map['coursenum'] = 0;
            $map['status'] = 3;
            $ordercount = $orderform->count();
            if($ordercount>0){
                $data['status'] = 5;
                $orderform->where($map)->save($data);
            }
        }
        
        $where = " 1 ";
        $url = '';
        
        //页码
        if(I('get.page')){
            $page = I('get.page');
        }else{
            $page = 1;
        }
        
        //订单状态搜索
        if(I('get.tab') >= 0 && I('get.tab') != ''){
            $filter['status'] = (int)I('get.tab');
            $url .= '&status=' . $filter['status'];
            $where .= " AND order_form.status =  " . $filter['status'];
        }else{
            $filter['status'] = '';
        }
        
        $limit = 8;
        $start = ($page - 1) * $limit; 
        
        $model = M('order_form');
        $data = $model->join('LEFT JOIN user u ON u.id = order_form.user_id')->join('LEFT JOIN requirement r ON r.id = order_form.requirement_id')->
                join('LEFT JOIN user_teacher t ON t.user_id = r.teacher_id')->join('LEFT JOIN user AS p ON p.id = r.teacher_id')->where($where)->order('r.id desc')
            ->limit($start,$limit)
            ->field('r.*,order_form.date,order_form.change_t,order_form.coursenum, order_form.requirement_id, order_form.id AS order_id, order_form.status AS order_status, t.relname, u.phone AS p_phone, p.phone,order_form.reback')->select();

        $total = $model->join('LEFT JOIN user u ON u.id = order_form.user_id')->join('LEFT JOIN requirement r ON r.id = order_form.requirement_id')->
                join('LEFT JOIN user_teacher t ON t.user_id = u.id')->where($where)->count();
        
        $pagination = new \Pub\Controller\PaginationController();
        $pagination->total = $total;
        $pagination->page = $page;
        $pagination->limit = $limit;
        $pagination->url = U('Order/index') . '?page={page}' . $url;        
        $render = $pagination->render();
        
        $new_url = U('Order/index') . '?page={page}' . $url;

        $this->assign('tab',I('get.tab'));
        $this->assign('nav','order');
        $this->assign('data',$data);
        $this->assign('render',$render);
        $this->assign('url',$new_url);
        $this->assign('filter',$filter);
        $this->display('order');
    }
    
    /*
     * 达成订单操作
     */
    public function deal(){
        $result = array(
            'flag' => 0,
            'content' => ''
        );
        if(IS_POST){
            $data['id'] = I('post.requriment_id');
            $data['status'] = I('post.status');
            $order_id = I('post.order_id');
            $model = M('requirement');
            $model->save($data);
            $order = M('order_form');
            $order->save(array('id'=> $order_id, 'status' => $data['status']));
            $result = array(
                'flag' => 1,
                'content' => '成功'
            );
        }
        $this->ajaxReturn($result);
    }

    /*
     * 更换老师
     */
    public function changeTeacher(){
        $teacher_id = trim(I('post.teacher_id'));
        $order_sn = trim(I('post.order_sn'));
        //0.验证老师id是否正确
        $user = M("user");
        $utmap['id'] = $teacher_id;
        $utmap['type'] = 1;
        $rs = $user->where($utmap)->count();
        if($rs!=1){
            $data['error'] = 1;
            $data['msg'] = '该用户不是老师';
            echo json_encode($data);die();
        }
        //1.修改订单信息
        $order_form = M('order_form');
        $map['order_sn'] = $order_sn;
        $data['teacher_id'] = $teacher_id;
        $rs1 = $order_form->where($map)->save($data);
        if(!$rs1){
            $data['error'] = 1;
            $data['msg'] = '修改订单表卡住';
            echo json_encode($data);die();
        }
        $requirement = M('requirement');
        $rs2 = $requirement->where($map)->save($data);
        if(!$rs2){
            $data['error'] = 1;
            $data['msg'] = '修改需求表卡住';
            echo json_encode($data);die();
        }
        //2.记录更换老师次数
        $rs3 = $order_form->where($map)->setInc('change_t',1);
        if(!$rs3){
            $data['error'] = 1;
            $data['msg'] = '更换老师出错';
            echo json_encode($data);die();
        }
        $data['error'] = 0;
        $data['msg'] = '更换老师成功';
        echo json_encode($data);
    }
    /*
     * 订单详情
     */
    public function detail(){
        //获取老师和家长详情
        $order_id = (int)I('get.id');
        $where = " order_form.id = ".$order_id;
        $model = M('order_form');
        $data = $model->join('LEFT JOIN user u ON u.id = order_form.user_id')->join('LEFT JOIN requirement r ON r.id = order_form.requirement_id')->
                join('LEFT JOIN user_teacher t ON t.user_id = u.id')->join('LEFT JOIN user AS p ON p.id = r.teacher_id')->where($where)->order('r.id desc')
                ->field('r.*, order_form.id AS order_id, order_form.status AS order_status,order_form.order_sn, t.relname, t.avatar, t.sex AS t_sex, t.grade AS t_grade, t.school AS t_school, t.address AS t_address, '
                        . 't.college AS t_college, t.year AS t_year, t.number AS t_number, t.score AS t_score, t.description AS t_description, t.tutor_experience AS t_tutor_experience, t.person_experience AS t_person_experience, u.phone, p.phone AS p_phone ')->find();
        if($data){
            if($data['t_grade']){
                $data['t_grade'] = $this->lesson(explode(",", $data['t_grade']),'grade');
            }
            if($data['t_lesson']){
                $data['t_lesson'] = $this->lesson(explode(",", $data['t_lesson']),'lesson');
            }
            if($data['t_school']){
                 $data['t_school'] = $this->lesson($data['t_school'],'school');
            }
            $data['avatar'] = htmlentities($data['avatar']);
        }
        //获取该订单记录
        $orderlog = M("orderlog");
        $map['orderlog.order_sn'] = array('like',$data['order_sn'].'%');
        $log = $orderlog->where($map)->order('orderlog.id desc')
            ->field('orderlog.order_sn,orderlog.coursenum,orderlog.coursenum_c,orderlog.eventtime,orderlog.remark,orderlog.money,orderlog.status')
            ->select();
        foreach($log as $k=>$v){
            $log[$k]['index'] = $k+1;
        }
        $this->assign('data',$data);
        $this->assign('lists',$log);
        $this->display('detail');
        
    }
    
    
    
}